<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="reservationNo">
        <el-input
          v-model="queryParams.reservationNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="渠道" prop="channel">
        <el-input
          v-model="queryParams.channel"
          placeholder="请输入渠道"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预订卡号" prop="guestNo">
        <el-input
          v-model="queryParams.guestNo"
          placeholder="请输入预订卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预订手机号" prop="guestPhone">
        <el-input
          v-model="queryParams.guestPhone"
          placeholder="请输入预订手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预订房间数" prop="reservationNum">
        <el-input
          v-model="queryParams.reservationNum"
          placeholder="请输入预订房间数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="押金" prop="deposit">
        <el-input
          v-model="queryParams.deposit"
          placeholder="请输入押金"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总金额" prop="money">
        <el-input
          v-model="queryParams.money"
          placeholder="请输入总金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预计入住日期" prop="checkInTime">
        <el-date-picker clearable
          v-model="queryParams.checkInTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择预计入住日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="预计退房日期" prop="checkOutTime">
        <el-date-picker clearable
          v-model="queryParams.checkOutTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择预计退房日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="入住类型(普通/钟点)" prop="checkType">
        <el-select v-model="queryParams.checkType" placeholder="请选择入住类型(普通/钟点)" clearable>
          <el-option
            v-for="dict in dict.type.hot_way_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="时长" prop="duration">
        <el-input
          v-model="queryParams.duration"
          placeholder="请输入时长"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单时间" prop="reservationTime">
        <el-input
          v-model="queryParams.reservationTime"
          placeholder="请输入下单时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hotal:reservation:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hotal:reservation:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hotal:reservation:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hotal:reservation:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reservationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户预订ID" align="center" prop="reservationId" />
      <el-table-column label="订单号" align="center" prop="reservationNo" />
      <el-table-column label="渠道" align="center" prop="channel" />
      <el-table-column label="预订客户id" align="center" prop="guestId" />
      <el-table-column label="预订卡号" align="center" prop="guestNo" />
      <el-table-column label="预订手机号" align="center" prop="guestPhone" />
      <el-table-column label="房间类型id" align="center" prop="roomTypeId" />
      <el-table-column label="预订房间数" align="center" prop="reservationNum" />
      <el-table-column label="押金" align="center" prop="deposit" />
      <el-table-column label="总金额" align="center" prop="money" />
      <el-table-column label="房间ids" align="center" prop="roomIds" />
      <el-table-column label="入住客人信息(json)" align="center" prop="guestDetail" />
      <el-table-column label="预计入住日期" align="center" prop="checkInTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkInTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计退房日期" align="center" prop="checkOutTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkOutTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入住类型(普通/钟点)" align="center" prop="checkType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hot_way_type" :value="scope.row.checkType"/>
        </template>
      </el-table-column>
      <el-table-column label="时长" align="center" prop="duration" />
      <el-table-column label="预订状态" align="center" prop="status" />
      <el-table-column label="下单时间" align="center" prop="reservationTime" />
      <el-table-column label="操作人id" align="center" prop="userId" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hotal:reservation:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hotal:reservation:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户预订对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单号" prop="reservationNo">
          <el-input v-model="form.reservationNo" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="渠道" prop="channel">
          <el-input v-model="form.channel" placeholder="请输入渠道" />
        </el-form-item>
        <el-form-item label="预订卡号" prop="guestNo">
          <el-input v-model="form.guestNo" placeholder="请输入预订卡号" />
        </el-form-item>
        <el-form-item label="预订手机号" prop="guestPhone">
          <el-input v-model="form.guestPhone" placeholder="请输入预订手机号" />
        </el-form-item>
        <el-form-item label="预订房间数" prop="reservationNum">
          <el-input v-model="form.reservationNum" placeholder="请输入预订房间数" />
        </el-form-item>
        <el-form-item label="押金" prop="deposit">
          <el-input v-model="form.deposit" placeholder="请输入押金" />
        </el-form-item>
        <el-form-item label="总金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入总金额" />
        </el-form-item>
        <el-form-item label="入住客人信息(json)" prop="guestDetail">
          <el-input v-model="form.guestDetail" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="预计入住日期" prop="checkInTime">
          <el-date-picker clearable
            v-model="form.checkInTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预计入住日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预计退房日期" prop="checkOutTime">
          <el-date-picker clearable
            v-model="form.checkOutTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预计退房日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入住类型(普通/钟点)" prop="checkType">
          <el-select v-model="form.checkType" placeholder="请选择入住类型(普通/钟点)">
            <el-option
              v-for="dict in dict.type.hot_way_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时长" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入时长" />
        </el-form-item>
        <el-form-item label="下单时间" prop="reservationTime">
          <el-input v-model="form.reservationTime" placeholder="请输入下单时间" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReservation, getReservation, delReservation, addReservation, updateReservation } from "@/api/hotal/reservation";

export default {
  name: "Reservation",
  dicts: ['hot_way_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户预订表格数据
      reservationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reservationNo: null,
        channel: null,
        guestId: null,
        guestNo: null,
        guestPhone: null,
        roomTypeId: null,
        reservationNum: null,
        deposit: null,
        money: null,
        roomIds: null,
        guestDetail: null,
        checkInTime: null,
        checkOutTime: null,
        checkType: null,
        duration: null,
        status: null,
        reservationTime: null,
        userId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户预订列表 */
    getList() {
      this.loading = true;
      listReservation(this.queryParams).then(response => {
        this.reservationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        reservationId: null,
        reservationNo: null,
        channel: null,
        guestId: null,
        guestNo: null,
        guestPhone: null,
        roomTypeId: null,
        reservationNum: null,
        deposit: null,
        money: null,
        roomIds: null,
        guestDetail: null,
        checkInTime: null,
        checkOutTime: null,
        checkType: null,
        duration: null,
        status: null,
        reservationTime: null,
        userId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.reservationId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户预订";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const reservationId = row.reservationId || this.ids
      getReservation(reservationId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户预订";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.reservationId != null) {
            updateReservation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReservation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const reservationIds = row.reservationId || this.ids;
      this.$modal.confirm('是否确认删除客户预订编号为"' + reservationIds + '"的数据项？').then(function() {
        return delReservation(reservationIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hotal/reservation/export', {
        ...this.queryParams
      }, `reservation_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
